﻿using System;
using System.Linq;
using System.ServiceProcess;
using Broadbeach.Core;

namespace Broadbeach.Service
{
    class Program
    {

        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (args.Where(a => a.IsMatch(@"(?i)^(-|/)i")).Any())
                {
                    Installer.Install(null, null);
                    return;
                }
                if (args.Where(a => a.IsMatch(@"(?i)^(-|/)u")).Any())
                {
                    Installer.Uninstall();
                    return;
                }
                if (args.Where(a => a.IsMatch(@"(?i)^(-|/)d")).Any())
                {
                    Run();
                    return;
                }
            }

            ServiceBase.Run(new Service(GetApplication()));
        }

        private static void Run()
        {
            var application = GetApplication();
            application.Start();

            Console.WriteLine("Press Enter to exit.");
            Console.ReadLine();

            application.Stop();
        }

        private static Application GetApplication()
        {
            var settings = Properties.Settings.Default;
            return new Application(
                settings.BaseAddress,
                settings.TeamProjectCollectionUri,
                settings.ScriptPath);
        }
    }
}
